<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
* Name:  Riders Model
*
* Author: John Schop, john.schop@gmail.com
*
*/

class Riders_model extends CI_Model
{
	

	public function __construct()
	{
		parent::__construct();
		$this->load->database();
	}
	

	/**
	 * Get all riders
	 *
	 * @access public
	 * @return object
	 */
	function get_all()
	{
		$this->db->order_by('riders.team_number', 'asc');
		$this->db->order_by('number', 'asc');
		$this->db->join('teams', 'teams.team_number = riders.team_number');
    $this->db->join('rider_types', 'rider_types.type_id = riders.type_id');
		return $this->db->get('riders')->result();
	}

  /**
   * Get rider by id
   *
   * @access public
   * @param string $rider_id
   * @return object rider object
   */
  function get_by_id($rider_id)
  {
      $this->db->join('teams', 'teams.team_number = riders.team_number');
      $this->db->join('rider_types', 'rider_types.type_id = riders.type_id');
      return $this->db->get_where('riders', array('rider_id' => $rider_id))->row();
  }

  function get_by_number($rider_number)
  { 
    $this->db->join('teams', 'teams.team_number = riders.team_number');
    $this->db->join('rider_types', 'rider_types.type_id = riders.type_id');
    return $this->db->get_where('riders', array('number' => $rider_number))->row();
  }

  function get_by_team_number($team_number)
  { 
    //$this->db->join('teams', 'teams.team_number = riders.team_number');
    $this->db->join('rider_types', 'rider_types.type_id = riders.type_id');
    $this->db->where('riders.starter = 1');
    return $this->db->get_where('riders', array('riders.team_number' => $team_number))->result();
  }  

  function get_all_by_team_number($team_number)
  { 
    //$this->db->join('teams', 'teams.team_number = riders.team_number');
    $this->db->join('rider_types', 'rider_types.type_id = riders.type_id');
    $this->db->order_by('starter', 'desc');
    $this->db->order_by('number', 'asc');
    //$this->db->where('riders.starter = 1');
    return $this->db->get_where('riders', array('riders.team_number' => $team_number))->result();
  }

	/**
	 * Get all active riders
	 *
	 * @access public
	 * @return object
	 */
	function get_all_active()
	{
		$this->db->order_by('riders.team_number', 'asc');
		$this->db->order_by('number', 'asc');
		$this->db->join('teams', 'teams.team_number = riders.team_number');
    $this->db->join('rider_types', 'rider_types.type_id = riders.type_id');
		$this->db->where('active', 1);
		return $this->db->get('riders')->result();
	}

	/**
	 * Get all starting riders
	 *
	 * @access public
	 * @return object
	 */
	function get_all_starters()
	{
		$this->db->order_by('riders.team_number', 'asc');
		$this->db->order_by('number', 'asc');
		$this->db->join('teams', 'teams.team_number = riders.team_number');
    $this->db->join('rider_types', 'rider_types.type_id = riders.type_id');
		$this->db->where('starter', 1);
		return $this->db->get('riders')->result();
	}

    /**
     * Update rider details
     *
     * @access public
     * @param int $rider_id
     * @param array $attributes
     * @return void
     */
    function update($rider_id, $attributes = array())
    {

        // Update
        if ($this->get_by_id($rider_id))
        {
            $this->db->where('rider_id', $rider_id);
            $this->db->update('riders', $attributes);
        }
        // Insert
        else
        {
            $this->db->insert('riders', $attributes);
        }

    }

  function get_types()
  {
    $this->db->order_by('type_description', 'asc');
    return $this->db->get('rider_types')->result_array();
  }

	function set_active($rider_id)
	{
        // Update

        $this->db->where('rider_id', $rider_id);
        $this->db->update('riders', array('active' => 1));
	}

	function set_inactive($rider_id)
	{
        // Update
        $this->db->where('rider_id', $rider_id);
        $this->db->update('riders', array('active' => 0));
	}

	function set_starter($rider_id)
	{
        // Update
        $this->db->where('rider_id', $rider_id);
        $this->db->update('riders', array('starter' => 1));
	}

	function set_no_starter($rider_id)
	{
        // Update
        $this->db->where('rider_id', $rider_id);
        $this->db->update('riders', array('starter' => 0));
	}

  function delete_all_results()
  {
    $riders = $this->get_all();
    $attributes = array('points_stage' => '0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0',
                        'points_stage_yellow' => '0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0',
                        'points_stage_green' => '0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0',
                        'points_stage_mountain' => '0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0'
                        );
    foreach ($riders as $rider)
    {
      $this->update($rider->rider_id, $attributes);
    }
  }

}
